//
//  HMHomeTableViewController.swift
//  Weibo02
//
//  Created by teacher on 16/4/16.
//  Copyright © 2016年 teacher. All rights reserved.
//

import UIKit




 let CellID = "weiboCell"

var KFooterH:CGFloat = 30

let KbackGroundColor:UIColor = UIColor(white: 220/255, alpha: 1)

class HMHomeTableViewController: VisitorController {
    
   
    
    let weiboListViewModel:HomeVCViewModel = HomeVCViewModel()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if  !UserAccountViewModel.shareUserAccountViewModel.isLogin {
             visitorView.setimageAndmessage(nil, message: nil)
            
           
            
            
            return
        }
        
        
        //添加了PDC控制器
        self.tableView.PD_addPDControll { () -> () in
            self.weiboListViewModel.loadWeiboList(self.activeIndector.isAnimating(),upDown: true, finished: { (weiboList) -> () in
                self.tableView.PD_endRefreshing()
                self.tableView.reloadData()
            })
        }
        
        //设置背景色
        self.tableView.backgroundColor = KbackGroundColor
       //自动计算行高
        tableView.estimatedRowHeight = 80
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None
        
        tableView.tableFooterView = activeIndector
        
        
        //设置边距
        KFooterH = activeIndector.frame.size.height
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -KFooterH, right:0 )
//        //设置contentsize 改变最大偏移量
//        tableView.contentSize = CGSize(width: tableView.contentSize.width, height: (tableView.tableFooterView?.frame.origin.y)!)
//
    tableView.registerClass(WeiboTableViewCell.self, forCellReuseIdentifier: CellID)
        
        weiboListViewModel.loadWeiboList(activeIndector.isAnimating(),upDown: false, finished: { (weiboList) -> () in
             self.tableView.reloadData()
            }) 
        
    }
    
    
    
    //MARK: - 懒加载控件
    lazy var activeIndector:UIActivityIndicatorView = {
       let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = UIColor.redColor()
        view.hidesWhenStopped = false
        return view
    }()
    
    
    //MARK: - scrollView的代理
    override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//        super.scrollViewDidEndDragging(scrollView, willDecelerate: true)
        //判断contentoffset
        if self.tableView.contentOffset.y > tableView.contentSize.height - tableView.bounds.size.height - activeIndector.frame.height + 100{
            //下载数据
            weiboListViewModel.loadWeiboList(activeIndector.isAnimating(),upDown: false, finished: { (weiboList) -> () in
                self.tableView.reloadData()
//                self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -KFooterH, right:0 )
                self.activeIndector.stopAnimating()
            })
            //启动刷新,修改内边距
            activeIndector.startAnimating()
//            tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: KFooterH + 10, right: 0)
            
            
        }
    }
    
    

    
}


//MARK - datasource 方法
extension HMHomeTableViewController{
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return  weiboListViewModel.weiboLists.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CellID, forIndexPath: indexPath) as! WeiboTableViewCell
//        cell.textLabel?.text = self.weiboListViewModel.weiboLists[indexPath.row].text
        
        cell.weiboCellModel = self.weiboListViewModel.weiboLists[indexPath.row]
        
        
        return cell
}

}






